Ext.namespace("Gfa.widgets.BaseGrid");
Gfa.widgets.BaseGrid.loadParent = function(parentConfig) {
	// 多表，载入主表信息
	var _parentDwrService = parentConfig.parentDwrService;
	var _parentLoadMethod = parentConfig.parentLoadMethod;
	var _parentHiddenId = parentConfig.parentHiddenId;
	var _parentEditFormId = parentConfig.parentEditFormId;
	var _parentId = Ext.get(_parentHiddenId).getValue();
	if (_parentId) {
		eval(_parentDwrService)[_parentLoadMethod](_parentId, function(result) {
			FormUtils.convertSimpleObject2Form(result, _parentEditFormId);
		});
	} else {
		FormUtils.clearForm(_parentEditFormId);
	}
}

	// 主子表保存
Gfa.widgets.BaseGrid.saveParentAndChildren = function() {
	var _beforeSaveFunc = this.parentConfig.beforeSaveFunc;
	var _parentId = this.parentConfig.parentId;
	var _parentHiddenId = this.parentConfig.parentHiddenId;
	var _parentDwrService = this.parentConfig.parentDwrService;
	var _parentLoadMethod = this.parentConfig.parentLoadMethod;
	var _parentSaveMethod = this.parentConfig.parentSaveMethod;
	var _parentEditFormId = this.parentConfig.parentEditFormId;
	var _sonsGrids = this.sonsGrids;

	// 主表校验
	if (_beforeSaveFunc) {
		var result = eval(_beforeSaveFunc + '()');
		if (!result == true) {
			return;
		}
	}
	// 子表校验
	var flag = false;
	Ext.each(_sonsGrids, function(sonGrid) {
		if (sonGrid.getSelected()) {
			var _beforeNewChildFunc = sonGrid.beforeNewChildFunc;
			if (_beforeNewChildFunc) {
				var result = eval(_beforeNewChildFunc + '()');
				if (!result == true) {
					flag =  true;
				}
			}
		}
	});
	
	if(flag){
		return;
	}

	// parent
	var obj = {};
	obj = FormUtils.convertSimpleForm2Object(_parentEditFormId, obj);
	if (obj[_parentId] != "" && obj[_parentId] != null
			&& obj[_parentId] != undefined) {
		// modified status
		obj.rowStatus = Gfa.GlobalConstant.ROWSTATUS_MODIFIED;
	} else {
		// new add status
		obj[_parentId] = null;
		obj.rowStatus = Gfa.GlobalConstant.ROWSTATUS_ADDED;
	}

	Ext.each(_sonsGrids, function(sonGrid) {
		// refresh son edit form
		sonGrid.refreshRowData(null, -1, null);
		// son
		var vSons = [];
		var vGridStoreDataItems = sonGrid.store.data.items;
		var vSonId = sonGrid.childId;
		var vParentName = sonGrid.parentName;
		var vForeignId = sonGrid.foreignId;
		Ext.each(vGridStoreDataItems, function(item) {
			var objj = item.data;
			if (objj[vSonId] == null || objj[vSonId] == undefined
					|| objj[vSonId] == '') {
				objj.rowStatus = Gfa.GlobalConstant.ROWSTATUS_ADDED;
				objj[vSonId] = null;
				if (vParentName
						&& (objj[vParentName] == null || objj[vParentName] == '')) {
					objj[vParentName] = obj;
				}
				if (vForeignId
						&& (objj[vForeignId] == null || objj[vForeignId] == '')) {
					objj[vForeignId] = obj[_parentId];
				}
			}
			objj.principalGroupCode = '00000000000';
			vSons[vSons.length] = objj;
		});
		obj[sonGrid.childSetName] = vSons;
	});
	// set PrincipalGroupCode
	obj.principalGroupCode = '00000000000';
	// real save
	eval(_parentDwrService)[_parentSaveMethod](obj, {
		callback : function(result) {
			FormUtils.convertSimpleObject2Form(result, _parentEditFormId);
			Ext.each(_sonsGrids, function(sonGrid) {
				sonGrid.store.load();
				sonGrid.showEditFormDiv(true);
			});
		}/*,//gfa4ext-all.js中DWR错误统一处理
		errorHandler : function(errorString, exception) {
			alert(errorString + ": " + exception);
		}*/
	});
};